﻿
package com.zh.authority.action;

import javacommon.util.JsonKit;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;
import javacommon.base.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;

import com.zh.authority.model.*;
import com.zh.authority.service.*;
import com.zh.authority.vo.query.*;
/**
 * @author grey2 email:grey2@126.com
 * @version 1.0
 * @since 1.0
 */
public class RoleAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	//forward paths
	protected static final String LIST_JSP= "/admin/pages/Role/list.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/admin/pages/Role/list.do";
	
	private RoleManager roleManager;
	private Role role;
	java.lang.Integer id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			role = new Role();
		} else {
			role = (Role)roleManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setRoleManager(RoleManager manager) {
		this.roleManager = manager;
	}	
	
	public Object getModel() {
		return role;
	}
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		RoleQuery query = newQuery(RoleQuery.class,DEFAULT_SORT_COLUMNS);
		
		Page page = roleManager.findPage(query);
		//savePage(page,query);
		StringBuffer roleJson = new StringBuffer("{\"total\":"+page.getTotalCount());
		roleJson.append(",\"rows\":")
				.append(JsonKit.toJson(page.getResult())+"}");
		this.renderJSON(roleJson.toString());
		return null;
	}
	
	/** 进入列表界面 */
	public String goListPage(){
		return LIST_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		roleManager.save(role);
		this.renderText("{\"success\":true,\"msg\":\"保存成功。\"}");
		return null;
	}
	
	/**保存更新对象*/
	public String update() {
		roleManager.update(this.role);
		this.renderText("{\"success\":true,\"msg\":\"保存成功。\"}");
		return null;
	}
	
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Integer id = new java.lang.Integer((String)params.get("id"));
			roleManager.removeById(id);
		}
		this.renderText("{\"success\":true,\"msg\":\"保存成功。\"}");
		return null;
	}

}
